package admin

import (
	"gf/app/model"
	"gf/app/service"
	"gf/library/response"
	"github.com/gogf/gf/net/ghttp"
)

var ArticleCategory = articleCategoryApi{}

type articleCategoryApi struct {

}

func (a *articleCategoryApi) List(r *ghttp.Request) {
	getListRes, _,err := service.ArticleCategory.List(r.Context(),0)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "ok", getListRes)
}

func (a *articleCategoryApi) Edit(r *ghttp.Request) {
	var (
		data *model.ArticleCategoryApiSaveReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, err := service.ArticleCategory.Edit(r.Context(), data)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "ok",result)
}


func (a *articleCategoryApi) Add(r *ghttp.Request) {
	var (
		data *model.ArticleCategoryApiReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	id, err := service.ArticleCategory.Add(r.Context(),data)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	res := struct {
		Id int64
	}{Id:id}

	response.JsonExit(r, 0, "ok", res)
}

func (a *articleCategoryApi) Del(r *ghttp.Request) {
	var (
		data *model.ArticleCategoryApiInfoReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	_, err := service.ArticleCategory.Del(r.Context(), data)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	res := struct {
		Id int
	}{Id:data.Id}
	response.JsonExit(r, 0, "ok",res)
}

